<template>
  <a-tree-select
    :treeData="treeData"
    :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
    :placeholder="placeholder || $t('m.common.label.option_select')"
    :disabled="disabled"
    @change="onChange"
    v-model="selectValue"
    :load-data="onLoadData"
  >
  </a-tree-select>
</template>

<script>

  import {getAction} from '@/api/manage';
  import YchConstant from "@/api/YchConstant";

  export default {
    name: "AppMenuUrlTag",
    props: {
      placeholder: {
        type: String,
      },
      disabled: {
        type: Boolean,
        default: false
      },
      allowClear: {
        type: Boolean,
        default: true
      },
      groupId:{
        type:String,
        required:true
      }
    },
    data() {
      return {
        treeData: [
          {
            key:'form',
            value:'form',
            title:this.$t('m.eform.formDesign.label.biao-dan'),
            selectable:false,
            isLeaf:false
          },
          {
            key:'view',
            value:'view',
            title:this.$t('m.eform.formDesign.label.ye-mian'),
            selectable:false,
            isLeaf:false
          },
          {
            key:'process',
            value:'process',
            title:this.$t('m.eform.formDesign.label.liu-cheng'),
            selectable:false,
            isLeaf:false
          },
          {
            key:'report',
            value:'report',
            title:this.$t('m.eform.formDesign.label.bao-biao'),
            selectable:false,
            isLeaf:false
          }
        ],
        url: {
          treeUrl: YchConstant.PREFIX_EFORM + "/manage/actAppGroup/getAppMenuUrlTreeByGroupId"
        },
        selectValue: undefined,
      }
    },
    methods: {
      onChange(value, label, extra) {
        let type = extra.triggerNode.dataRef.attributes.type
        this.$emit('change', value, type, label)
        this.selectValue = null
      },
      onLoadData(treeNode) {
        let url = "";
        let param = {
          groupId: this.groupId,
          pageNo:1,
          pageSize:10000
        }
        if(treeNode.value=='form'){
          url = YchConstant.PREFIX_EFORM + "/manage/actAppForm/list"
        }else if(treeNode.value=='view'){
          url = YchConstant.PREFIX_EFORM + "/manage/actAppView/listPcView"
        }else if(treeNode.value=='process'){
          param = {
            catalogId: this.groupId,
            pageNo:1,
            pageSize:10000
          }
          url = YchConstant.PREFIX_BPM + "/bpm-back/processDefRest/list/v1"
        }else if(treeNode.value=='report'){
          url = YchConstant.PREFIX_EFORM + "/report/actReportWidget/list"
        }
        return new Promise(resolve => {
          if (treeNode.dataRef.children) {
            resolve();
            return;
          }
          this.loading = true
          getAction(url, param).then((res) => {
            if (res.success) {
              if(treeNode.value=='form'){
                treeNode.dataRef.children = this.formToTreeData(res.result.records)
              }else if(treeNode.value=='view'){
                treeNode.dataRef.children = this.viewToTreeData(res.result.records)
              }else if(treeNode.value=='process'){
                treeNode.dataRef.children = this.processToTreeData(res.result.records)
              }else if(treeNode.value=='report'){
                treeNode.dataRef.children = this.reportToTreeData(res.result.records)
              }
              this.treeData = [...this.treeData];
              resolve();
            } else {
              this.messageUtil.warning(res.message)
            }
          }).finally(() => {
            this.loading = false
          })
        });
      },
      formToTreeData(list){
        if(list && list.length && list.length>0){
          return list.map(item=>{
            return {
              key:item.formCode,
              value:item.formCode,
              title:item.formName,
              isLeaf:true,
              attributes:{
                type:'form'
              }
            }
          })
        }else{
          return []
        }
      },
      viewToTreeData(list){
        if(list && list.length && list.length>0){
          return list.map(item=>{
            return {
              key:item.viewCode,
              value:item.viewCode,
              title:item.viewName,
              isLeaf:true,
              attributes:{
                type:'view'
              }
            }
          })
        }else{
          return []
        }
      },
      processToTreeData(list){
        if(list && list.length && list.length>0){
          return list.map(item=>{
            return {
              key:item.id,
              value:item.id,
              title:item.name,
              isLeaf:true,
              attributes:{
                type:'process'
              }
            }
          })
        }else{
          return []
        }
      },
      reportToTreeData(list){
        if(list && list.length && list.length>0){
          return list.map(item=>{
            return {
              key:item.widgetCode,
              value:item.widgetCode,
              title:item.widgetName,
              isLeaf:true,
              attributes:{
                type:'report'
              }
            }
          })
        }else{
          return []
        }
      },
    },
  }
</script>
